﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using d1Common.WebController;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using d1Common;
using d1Project.Modules.Common.do4A.Models;
using d1Common.Interface;
using d1Project.Modules.Common.do4A.Domain;

namespace d1Project.Modules.Common.do4A.Controllers.a1
{
    [Route("do4A/a1/RolePermission")]
    public class RolePermissionController : Authorize1Controller
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private do4ADbContext db = new do4ADbContext();

        [PermissionCheck(Id = "do4A$Role", Operation = 0)]
        public IHttpActionResult Get(string RoleId)
        {
            IDictionary<string, IRolePermission> PermissionList = PermissionMgr.Instance.GetRolePermissionsLocal(RoleId);

            List<RolePermissionsListVm> list = new List<RolePermissionsListVm>();
            foreach (var item in PermissionList)
            {
                RolePermissionsListVm rolepermissions = new RolePermissionsListVm();
                rolepermissions.PermissionId = item.Value.PermissionId;
                rolepermissions.Operations = item.Value.Operations;
                list.Add(rolepermissions);
            }
            return Ok(list);
        }

        [PermissionCheck(Id = "do4A$Role", Operation = 8)]
        public async Task<IHttpActionResult> Put(RolePermissionsVm model)
        {
            var old = await db.RolePermissions.Where(t => t.RoleId == model.RoleId).ToListAsync();
            if (old != null && old.Count > 0) db.RolePermissions.RemoveRange(old);

            if (model.Permissions != null && model.Permissions.Count >= 1)
            {
                List<RolePermission> list = new List<RolePermission>();
                foreach (var item in model.Permissions)
                {
                    RolePermission roleper = new RolePermission();
                    roleper.Id = doServices.Instance.DoCore.getGUID();
                    roleper.RoleId = model.RoleId;
                    roleper.PermissionId = item.PermissionId;
                    roleper.Operations = item.Operations;
                    list.Add(roleper);
                }
                db.RolePermissions.AddRange(list);
            }
            await db.SaveChangesAsync();

            //更新缓存
            IDictionary<string, IRolePermission> _results = PermissionMgr.Instance.GetRolePermissionsLocal(model.RoleId);
            PermissionMgr.Instance.RolePermisssions[model.RoleId] = _results;

            return Ok();
        }
    }

    public class RolePermissionsVm
    {
        public string RoleId { get; set; }
        public List<PermissionsList> Permissions { get; set; }
    }

    public class RolePermissionsListVm
    {
        public string PermissionId { get; set; }
        public int Operations { get; set; }
    }

    public class PermissionsList
    {
        /// <summary>
        /// 权限Id
        /// </summary>
        public string PermissionId { get; set; }

        /// <summary>
        /// 操作权限 (所有操作项按位与的结果) 
        /// </summary>
        public int Operations { get; set; }

    }


}